import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o visible = false;
    @o isUpdate = false;
    @o roleId = '';
    @o roleName = '';
    @o roleRemarks = '';
    @o roleSign = '';
    @o menuListForRole = [];
    @o allMenuList = [];

    will() {

    }

    closeDialog() {
        this.visible = false;
        this.isUpdate = false;
        this.roleId = '';
        this.roleName = '';
        this.roleRemarks = '';
        this.roleSign = '';
        this.menuListForRole = [];
    }

    getAllMenuList() {
        request({url: $$.api('getAllMenuList')}).then(res => {
            this.allMenuList = res.data;
        }).catch(err => {
            this.stores.Message.showMessage(2, err.message);
        });
    }

    roleNameChange(v) {
        this.roleName = v;
    }

    roleSignChange(v) {
        this.roleSign = v;
    }

    roleRemarksChange(v) {
        this.roleRemarks = v;
    }

    selectMenu(id) {
        if (!!~this.menuListForRole.indexOf(id)) {
            this.menuListForRole = this.menuListForRole.filter(item => {
                return item !== id;
            });

        } else {
            this.menuListForRole.push(id);
        }
    }

    getMenuListForRole() {
        this.stores.Frame.showLoading();
        request({url: $$.api('getMenuListForRole') + this.roleId}).then(res => {
            this.stores.Frame.hideLoading();
            res.data && res.data.map(item => {
                this.menuListForRole.push(item.id);
            })
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }

    checkInput() {
        if (!this.roleName || (!!this.roleName && !this.roleName.toString().trim())) {
            this.stores.Message.showMessage(3, '身份名称不能为空');
            return true;
        }
        if (!this.roleSign || (!!this.roleSign && !this.roleSign.toString().trim())) {
            this.stores.Message.showMessage(3, '身份标识不能为空');
            return true;
        }
    }

    confirmDialog() {
        if (this.checkInput()) {
            return;
        }
        this.stores.Frame.showLoading();
        request({
            url: this.isUpdate ? $$.api('roleUpdate') : $$.api('roleInsert'),
            method: 'post',
            data: {
                id: this.roleId || null,
                menuIds: this.menuListForRole,
                name: this.roleName,
                sign: this.roleSign,
                remarks: this.roleRemarks
            }
        }).then(res => {
            this.closeDialog();
            this.stores.Frame.hideLoading();
            this.fire('rolesDialog:update');
            this.stores.Message.showMessage(1, res.message);
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }

    mount() {
        this.getAllMenuList();
        this.on('rolesDialog:show', (data) => {
            this.visible = true;
            if (data) {
                this.isUpdate = true;
                this.roleId = data.id;
                this.roleName = data.name;
                this.roleRemarks = data.remarks;
                this.roleSign = data.sign;
                this.getMenuListForRole();
            }
        });
    }

}

export default Store;